package com.tuanzi.loan.web.bean.statistics;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.joda.time.DateTime;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.service.statistics.ActivityAverageJoinInterceptStatService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.statistics.ActivityAverageJoinInterceptStatRequest;
import com.tuanzi.loan.vo.response.statistics.ActivityAverageJoinInterceptStatResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.ActivityAverageJoinInterceptStatLazyDataModel;
import com.tuanzi.server.loan_base.model.bean.common.CommAdvertise;
import com.tuanzi.server.loan_base.model.bean.common.CommPlatform;
import com.tuanzi.server.loan_base.model.bean.common.CommUserType;
import com.tuanzi.server.loan_base.model.db.CommProductChannel;
import com.tuanzi.server.loan_base.model.db.CommProductPrdid;
import com.tuanzi.server.loan_base.model.db.LoanProductInfo;

/**
 * 人均参与截流统计
 * @author weibifeng 
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ActivityAverageJoinInterceptStatBean extends JSFListingBean<ActivityAverageJoinInterceptStatRequest, ActivityAverageJoinInterceptStatResponse> {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	
    @Autowired
    ActivityAverageJoinInterceptStatService activityAverageJoinInterceptStatService;
    
    private StreamedContent xlsFile;

    private List<CommAdvertise> adList;
    private List<LoanProductInfo> activityList;
    private List<CommProductPrdid> productList;
    private List<CommProductChannel> channelList;
    private List<CommUserType> userTypeList;
    private List<CommPlatform> platformList;
    private TreeNode prodNodeTree;
    
    public StreamedContent downloadExcel() throws UnsupportedEncodingException {
    	InputStream input = new ByteArrayInputStream(activityAverageJoinInterceptStatService.getExcel($).getBytes("GBK"));
		String fileName = System.currentTimeMillis() + ".csv";
		xlsFile = new DefaultStreamedContent(input, "application/vnd.ms-excel;charset=gb2312", fileName);
    	return xlsFile;
    }
    

    @PostConstruct
    private void init() {
        initLazyDataModel();
        initList();
    }

    private void initList(){
        adList = activityAverageJoinInterceptStatService.getAD();
        activityList = activityAverageJoinInterceptStatService.getActivity();
        productList = activityAverageJoinInterceptStatService.getProduct();
        channelList = activityAverageJoinInterceptStatService.getChannel();
        userTypeList = activityAverageJoinInterceptStatService.getUserType();
        platformList = activityAverageJoinInterceptStatService.getPlatform();
        prodNodeTree = activityAverageJoinInterceptStatService.getProductTree();
    }

    public void onFlushCache(){
    	activityAverageJoinInterceptStatService.cleanCache();
        initList();
        addInfoMessage("缓存更新成功!");
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onRefresh(){
        lazyDataModel = new ActivityAverageJoinInterceptStatLazyDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new ActivityAverageJoinInterceptStatRequest();
        
        // 初始化默认当前日期
        Date date = null;
        try {
			date = sdf.parse(sdf.format(new DateTime().plusDays(-1).toDate()));
			$.setStartDate(date);
	        $.setEndDate(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        
        // 初始化默认选择全部
        List<Integer> defaultInt = new ArrayList<Integer>();
        defaultInt.add(-1000);
        $.setProductIdList(defaultInt);
        $.setUserTypeList(defaultInt);
        $.setAdIdList(defaultInt);
        
        List<Long> defaultLong = new ArrayList<Long>();
        defaultLong.add(-1000L);
        $.setActivityList(defaultLong);
        
        List<String> defaultStr = new ArrayList<String>();
        defaultStr.add("-1000");
        $.setPlatformList(defaultStr);
        $.setChannelIdList(defaultStr);
        
        CommProductPrdid defaultProd = new CommProductPrdid();
        defaultProd.setPrdid(-1000);
        defaultProd.setName("全部");
        defaultProd.setLevel(2);
        TreeNode defNode = new DefaultTreeNode(defaultProd);
        defNode.setSelectable(true);
        defNode.setSelected(true);
        TreeNode[] treeNodes = {defNode};
        $.setProdNodes(treeNodes);
        
        lazyDataModel = new ActivityAverageJoinInterceptStatLazyDataModel($);
    }
    
}




